<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>用户管理</title>
    <link rel="stylesheet" href="/res/layui/css/layui.css" media="all">
    <script src="/res/layui/layui.js"></script>
    <script src="/res/js/jquery.min.js"></script>
    <style>
        body{margin: 10px;}
        .demo-carousel{height: 200px; line-height: 200px; text-align: center;}

        .layui-form-item .layui-input-inline {
            float: left;
            width: 140px;
            margin-right: 10px;
        }
    </style>
</head>
<body>

<div class="list_main">

    <div class="list_query">
        <form id="queryForm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" id="name"  autocomplete="off" placeholder="请输入用户名" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">

                    <div class="layui-input-inline">
                        <input type="button" class="layui-btn" value="查询" id="query_btn">
                    </div>
                </div>
            </div>
        </form>
    </div>

    <table class="layui-hide" id="demo" lay-filter="test"></table>

</div>

<img id="img_" style="display: none;position: absolute;top:120px;left:200px;">

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">增加</button>
        <button class="layui-btn  layui-btn-danger  layui-btn-sm" lay-event="delete">删除</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

</script>




<script>
    var table ;
    layui.use([ 'layer', 'table','laydate','jquery'], function(){
        var layer = layui.layer ;//弹层
        var $ = layui.$ ;//使用内置jquery
        var laydate = layui.laydate ;
        table = layui.table //表格
        //提示信息
        //layer.msg('数据正在加载!');


        //执行一个 table 实例  渲染
        table.render({
            elem: '#demo'
            ,id:'dataListTable'
            ,height: 600
            ,url: '/person/findAll.action' //数据接口,数据格式是什么样的?
            ,title: '用户表'
            ,page: true //开启分页
            ,toolbar: '#toolbarDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,totalRow: true //开启合计行
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: 'ID', width:60, sort: true, fixed: 'left'}
                ,{field: 'name', title: '用户名', width:120}
                ,{fixed: 'right', width: 165, align:'center', toolbar: '#barDemo'}
            ]]


        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    //layer.msg('添加');
                    //iframe层-父子操作
                    layer.open({
                        type: 2,
                        area: ['700px', '600px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/pages/person/form.html'
                    });
                    break;

                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        //layer.msg('删除');
                        layer.confirm('真的删除行么', function(index) {
                            //将多个ID使用,连接.传到后台
                            console.info(data);
                            let ids = "";
                            for (let i = 0; i < data.length; i++) {
                                ids += data[i].id + ",";
                            }
                            //去掉,
                            ids = ids.substr(0, ids.length - 1);
                            console.info("取到的ID是:" + ids);

                            let data_ = {ids: ids};

                            let url = "/person/deleteAll.action"
                            $.post(url, data_, function (resp) {
                                layer.msg(resp.msg);
                                if (resp.state >0) {
                                    //重新加载表格
                                    table.reload('dataListTable');
                                }
                            }, "json");


                            // $.each(data,function (index,item) {
                            //     ids += item.userId + ",";
                            // })
                        });
                    }
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'detail'){
                layer.msg('查看操作');
            } else if(layEvent === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);//关闭弹出框
                    //向服务端发送删除指令

                    let url = "/person/delete.action"
                    let data_ = {id:data.id}; //传入的参数
                    $.post(url,data_,function(p){
                        if(p.state>0){
                            layer.msg('删除成功');
                        }else{
                            layer.msg('删除失败');
                        }
                    },"json");


                });
            } else if(layEvent === 'edit'){
                //layer.msg('编辑操作');
                //iframe层-父子操作
                layer.open({
                    type: 2,
                    area: ['700px', '600px'],
                    fixed: false, //不固定
                    maxmin: true,
                    //content: '/pages/book/form.html?id='+data.userId,
                    content: '/pages/person/form.html?id='+data.id,
                    success:function (layero,index) {
                        //当页面弹出完成时,怎么做
                        //alert(data.userName);
                    }
                });


            }
        });

    });




    function showImg(obj) {
        document.getElementById("img_").src=obj.src;
        document.getElementById("img_").style.display="block";
    }

    function hideImg() {
        document.getElementById("img_").style.display="none";
    }
    
    
    //导入jquery
    $(function () {
        $("#query_btn").on("click",function () {
            //执行查询
            var data_ ={
                name:$("#name").val()
            };
            //执行重载
            table.reload('dataListTable', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: data_ //需要传到后台的参数
            });

        })
    })


</script>
</body>
</html>
